function loadMoreNewsItems(data) {
	var dataType = typeof data;
	var newsFeedId = $('#all_news').is(':visible') ? 'all_news' : 'my_news';
	var newsFeedType = $('#all_news').is(':visible') ? '0' : '1';
	var offset = $('#all_news').is(':visible') ? newAllOffset : newMyOffset;
	var that = this;

	// If endless scroll plugin called this function, but endless scroll is disabled, the news feed should not be updated.
	if(dataType == 'number' && !endlessScrollEnabled) {
		that.fired = false;	// property used by endless scroll plugin; check plugin source for usage information.
		return;
	}

	if ($('#filter_all_news').is(':checked')) {
		if(allEndlessScrollingDone) {
			that.fired = false;
			return;
		}
		$('#showMoreAll').hide();
	}
	else {
		if(myEndlessScrollingDone) {
			that.fired = false;
			return;
		}
		$('#showMoreMy').hide();
	}

	$('#spinner').css('visibility','visible');

	$.ajax({
		 type: 'GET',
		 url: API2_PATH+'news_feed',
		 data: { user_id: user_id, offset: offset, per_page: items_pp, type: newsFeedType },
		 success: function(data) {
			 ga_track('News Feed', 'load more', data);
			 if (data.events=='') {
				 if ($('#all_news').is(':visible')) {
					 allEndlessScrollingDone = true;
				 }
				 else {
					 myEndlessScrollingDone = true;
				 }
			 }
			 else {
				 if ($('#all_news').is(':visible')) {
					 newAllOffset = data.newOffset;
				 }
				 else {
					 newMyOffset = data.newOffset;
				 }
				 $('#'+newsFeedId).append(data.events);
				 $('#'+newsFeedId).find('.message, .description').truncate({ max_length: MAX_TEXT_LENGTH });
			 }
			 $('#spinner').css('visibility', 'hidden');
			 if (data.events!='') {
				 if ($('#filter_all_news').is(':checked')) {
					 $('#showMoreAll').show();
				 }
				 else {
					 $('#showMoreMy').show();
				 }
			 }
			 else {
				 $('#no_more_'+newsFeedId).show();
			 }
			 endlessScrollEnabled = true;
			 that.fired = false;
		 }
	 });
}

function noMoreNewsItems() {
	return(allEndlessScrollingDone && myEndlessScrollingDone);
}

var MAX_TEXT_LENGTH = 400;

var newAllOffset = all_offset;
var newMyOffset = my_offset;
var endlessScrollEnabled = true;
var allEndlessScrollingDone = (all_offset==-1 ? true : false);
var myEndlessScrollingDone = (my_offset==-1 ? true : false);
var myNewsLazyLoadDone = false;
var endlessScrollOptions = {
	bottomPixels: 425,
	fireOnce: true,
	fireDelay: 1000,
	insertAfter: '.news_feed_container',
	loader: '',
	callback: loadMoreNewsItems,
	ceaseFire: noMoreNewsItems
};

$('.message, .description').truncate({ max_length: MAX_TEXT_LENGTH });

$(document).ready(function() {
	$('#filter_news').buttonset();
	$('#spinner').css('visibility','hidden');

	$('#filter_all_news').click(function() {
		ga_track('News Feed', $(this).attr('id'));
		if ($('.'+$(this).attr('id')).is(':visible')) { return false; }	// do nothing if user reclicks the same button
		$('#filter_my_news').removeAttr('checked');
		$('#filter_all_news').attr('checked', 'checked');
		$('.news_feed').toggle();
		$('.no_more').toggle();
	});

	$('#filter_my_news').click(function(data) {
		ga_track('News Feed', $(this).attr('id'));
		if ($('.'+$(this).attr('id')).is(':visible')) { return false; }	// do nothing if user reclicks the same button
		$('#filter_all_news').removeAttr('checked');
		$('#filter_my_news').attr('checked', 'checked');
		$('.news_feed').toggle();
		$('.no_more').toggle();
		if (!myNewsLazyLoadDone) {
			loadMoreNewsItems(data);
			myNewsLazyLoadDone = true;
		}
	});

	$('#showMoreAll').click(function(data) {
		endlessScrollEnabled = false;
		loadMoreNewsItems(data);
		return false;
	});

	$('#showMoreMy').click(function(data) {
		endlessScrollEnabled = false;
		loadMoreNewsItems(data);
		return false;
	});

	$('.event_group_show').live('click', function() {
		ga_track('News Feed', 'group expanded');
		$(this).parents('tr').next('.parent').show();
		var newsFeedContainer = $(this).parents('tr').next('.parent').find('#similarNewsFeedContainer');
		var numberOfRows = $(this).parents('tr').next('.parent').find('#newsHidden >tbody >tr').length;
		var animationTime = calcSlideTime(numberOfRows);
		var olderIE;
		if ($.browser.msie && $.browser.version<9){olderIE=true;}
		if(!olderIE){newsFeedContainer.css('opacity', 0);}
		newsFeedContainer.slideDown(animationTime);
		if (!olderIE){
			newsFeedContainer.animate(
				{ opacity: 1 },
				{ queue: false, duration: 'slow' });
		}
		$(this).parent().find('.event_group_hide').show();
		$(this).hide();
		return false;
	});

	$('.event_group_hide').live('click', function() {
		ga_track('News Feed', 'group collapsed');
		$(this).parents('tr').next('.parent').hide();
		var newsFeedContainer = $(this).parents('tr').next('.parent').find('#similarNewsFeedContainer');
		var numberOfRows = $(this).parents('tr').next('.parent').find('#newsHidden >tbody >tr').length;
		var animationTime = calcSlideTime(numberOfRows);
		newsFeedContainer.stop(false, true);
		if (numberOfRows>10) {animationTime = 'slow';}
		newsFeedContainer.slideUp(animationTime);
		$(this).parent().find('.event_group_show').show();
		$(this).hide();
		return false;
	});

	function calcSlideTime(rowNumber) {
		if (rowNumber>0) {
			if (rowNumber<80) {
				return (100*rowNumber);
			}
			else {
				return 'slow';
			}
		}
		return false;
	}

	$('.picframe').tipsy({gravity:'w', html:true, delayIn:200});

	trackRecommendations('a.titlesmall');

	if ($.browser.msie && $.browser.version<9) {
		$(window).endlessScroll(endlessScrollOptions);
	}
	else {
		$(document).endlessScroll(endlessScrollOptions);
	}

    var topOffset = 55;
    var topOverlap = 10;
    var top = $('#recommended_users').offset().top - topOffset + topOverlap;
    $(window).scroll(function (event) {
        if ($(this).scrollTop() > top) {
            $('#recommended_users').addClass('panel_fixed');
        }
        else {
            $('#recommended_users').removeClass('panel_fixed');
        }
    });
});